package tech.dx.springdb.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.dx.springdb.entity.ClassType;
import tech.dx.springdb.service.ClassTypeService;

import java.util.Optional;

/**
 * @ClassName ClassTypeController
 * @Description TODO
 * @Author daoxuan
 * @Date 2019/4/1 18:22
 * @Version 1.0
 */
@RestController
@RequestMapping("classtype")
@Api(tags = {"上课类型 本校区 合作小区等"})
public class ClassTypeController {

    @Autowired
    private ClassTypeService classTypeService;

    @GetMapping("/{id}")
    public ClassType get(@PathVariable long id) {
        Optional<ClassType> byId = classTypeService.getById(id);
        if (byId.isPresent()) {
            return byId.get();
        } else {
            return null;
        }
    }

    @PostMapping
    public ClassType add(@RequestBody ClassType classType) {
        return classTypeService.add(classType);
    }

    @PutMapping("/{id}")
    public ClassType update(@RequestBody ClassType classType, @PathVariable long id) {
        if (classTypeService.isExitById(id)) {
            classType.setId(id);
            return classTypeService.update(classType);
        } else {
            return null;
        }
    }

    @DeleteMapping("/{id}")
    public void del(@PathVariable long id) {
        if (classTypeService.isExitById(id)) {
            classTypeService.del(id);
        }
    }
}
